package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.clients.ItemClient;
import com.hmall.common.clients.UserClient;
import com.hmall.common.dto.Address;
import com.hmall.common.pojo.Item;
import com.hmall.common.util.ThreadLocalUtil;
import com.hmall.order.config.RabbitMqConfig;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.OrderRequest;
import com.hmall.order.service.IOrderService;
import com.hmall.order.service.OrderDetailService;
import com.hmall.order.service.OrderLogisticsService;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.commons.util.IdUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private UserClient userClient;
    @Autowired
    private OrderDetailService detailService;
    @Autowired
    private OrderLogisticsService logisticsService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    @Transactional
    public Long createOrder(OrderRequest orderRequest) {
        Order order = new Order();
        Item item = itemClient.getById(orderRequest.getItemId());
        //计算商品总价
        order.setTotalFee(item.getPrice() * orderRequest.getNum());
        //初识状态为未支付
        order.setStatus(1);
        //付款方式
        order.setPaymentType(orderRequest.getPaymentType());
        //获取用户id，封装order
        String userId = ThreadLocalUtil.getThreadLocal();
        order.setUserId(Long.valueOf(userId));
        save(order);

        //orderDetail
        OrderDetail orderDetail = new OrderDetail();
        //orderId
        orderDetail.setOrderId(order.getId());
        //itemId
        orderDetail.setItemId(item.getId());
        //购买数量
        orderDetail.setNum(orderRequest.getNum());
        //商品名
        orderDetail.setTitle(item.getName());
        //单价
        orderDetail.setPrice(item.getPrice());
        //商品规格
        orderDetail.setSpec(item.getSpec());
        //图片
        orderDetail.setImage(item.getImage());
        detailService.save(orderDetail);

        //orderLogistics
        Address address = userClient.findAddressById(orderRequest.getAddressId());

        OrderLogistics orderLogistics = new OrderLogistics();
        //orderId
        orderLogistics.setOrderId(order.getId());
        //收件人姓名
        orderLogistics.setContact(address.getContact());
        //手机号
        orderLogistics.setPhone(address.getMobile());
        //省
        orderLogistics.setProvince(address.getProvince());
        //市
        orderLogistics.setCity(address.getCity());
        //区
        orderLogistics.setTown(address.getTown());
        //街道
        orderLogistics.setStreet(address.getStreet());
        logisticsService.save(orderLogistics);

        //扣库存
        itemClient.stock(item.getId(), orderRequest.getNum());

        rabbitTemplate.convertAndSend("ttl.topic", "ttl", order.getId());


        return order.getId();
    }

}
